package com.gis.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gis.dto.NodeDataDTO;
import com.gis.dto.ProcessDataDTO;
import com.gis.entity.NodeData;
import com.gis.entity.ProcessData;
import com.gis.result.ResponseResult;
import com.gis.service.NodeService;
import com.gis.service.ProcessService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "[算法]流程", description = "流程数据接口")
@RequestMapping("/script")
public class ProcessController {
    @Autowired
    private ProcessService processService;

    @Autowired
    private NodeService nodeService;
    
    @Operation(summary = "添加流程")
    @PostMapping("/addProcess")
    public ResponseResult<String> addProcess(@RequestBody ProcessData process) {
        boolean result = processService.save(process);
        if (result) {
            return ResponseResult.success("添加成功");
        } else {
            return ResponseResult.fail("添加失败");
        }
    }

    @Operation(summary = "删除流程")
    @Parameters({
        @Parameter(name = "processId", description = "流程ID", required = true)
    })
    @DeleteMapping("/deleteProcess")
    public ResponseResult<String> deleteProcess(@RequestParam Long processId) {
        QueryWrapper<NodeData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("process_id", processId);
        List<NodeData> nodes = nodeService.list(queryWrapper);
        nodeService.removeBatchByIds(nodes);
        boolean result = processService.removeById(processId);

        if (result) {
            return ResponseResult.success("删除成功");
        } else {
            return ResponseResult.fail("删除失败");
        }
    }

    @Operation(summary = "获取所有流程")
    @GetMapping("/getAllProcess")
    public ResponseResult<List<ProcessData>> getAllProcess() {
        List<ProcessData> processes = processService.list();
        return ResponseResult.success(processes);
    }

    @Operation(summary = "根据Id获取流程")
    @GetMapping("/getProcessById")
    public ResponseResult<ProcessData> getProcessById(@RequestParam Long id) {
        ProcessData processData = this.processService.getByProcessId(id);
        if(processData != null){
            return ResponseResult.success(processData);
        }else{
            return ResponseResult.fail("未找到流程");
        }
    }

    @Operation(summary = "添加流程和节点")
    @PostMapping("/addProcessAndNodes")
    public ResponseResult<Object> addProcessAndNodes(@RequestBody List<NodeData> nodes, @RequestParam String processName) {
        ProcessData processData = new ProcessData();
        processData.setProcessName(processName);

        boolean result = processService.save(processData);
        if (result) {
            return ResponseResult.success("添加成功");
        } else {
            return ResponseResult.fail("添加失败");
        }
    }

    @Operation(summary = "获取所有流程及其节点")
    @GetMapping("/getAllProcessWithNodes")
    public ResponseResult<List<ProcessData>> getAllProcessWithNodes() {
        List<ProcessData> processes = processService.getAllProcessWithNodes();
        return ResponseResult.success(processes);
    }

    @Operation(summary = "用节点添加流程")
    @PostMapping("/addProcessWithNodes")
    public ResponseResult<String> addProcessWithNodes(@RequestBody ProcessDataDTO processDataDTO) {
        try{
            ProcessData process = new ProcessData();
            List<NodeDataDTO> nodes = processDataDTO.getNodes();
            log.info("processDataDTO: {}", processDataDTO);
            BeanUtils.copyProperties(processDataDTO, process);
            
            processService.save(process);
            for (NodeDataDTO nodeDataDTO : nodes) {
                NodeData node = new NodeData();
                BeanUtils.copyProperties(nodeDataDTO, node);
                node.setProcessId(process.getId());
                nodeService.save(node);
            }
        } catch (Exception e) {
            String message = e.getMessage();
            return ResponseResult.fail(message);
        }
        

        return ResponseResult.success("添加成功");
    }
}
